package com.lottery.ruok.lottrey.chat.event.handler;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.applozic.mobicomkit.ChatConfig;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.GetPacketResultActivity;
import com.lottery.ruok.lottrey.activity.GiveRewardActivity;
import com.lottery.ruok.lottrey.activity.GiveRewardDetailActivity;
import com.lottery.ruok.lottrey.activity.LoginActivity;
import com.lottery.ruok.lottrey.chat.RongCloudChatManager;
import com.lottery.ruok.lottrey.chat.model.prize.message.PrizeMessage;
import com.lottery.ruok.lottrey.chat.model.redpacket.message.receiveRedPacketMessage.ReceiveRedPacketMessage;
import com.lottery.ruok.lottrey.chat.model.redpacket.message.sendRedPacketMessage.RedPacketMessage;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.dialog.RedPacketAniDialog;
import com.lottery.ruok.lottrey.eventbusObject.GrabRedPacketSucess;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GrabGiftRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GrabGiftResponse;

import org.greenrobot.eventbus.EventBus;

import io.rong.imkit.userInfoCache.RongUserInfoManager;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.UserInfo;
import utils.StringUtils;

public class OnConversationClickListenerImpl implements OnConversationClickListener {
    @Override
    public void onSendPrize(Context context, Conversation.ConversationType conversationType, UserInfo user, String targetId) {
        Intent intent = new Intent(context, GiveRewardActivity.class);
        intent.putExtra(GiveRewardActivity.EXTRA_CONVERSATION_TYPE, conversationType);
        intent.putExtra(GiveRewardActivity.EXTRA_USER, user);
        intent.putExtra(GiveRewardActivity.EXTRA_TARGET_ID, targetId);
        context.startActivity(intent);
    }

    @Override
    public void onReceiveRedPacket(final Context context, View view, final RedPacketMessage content, final Message message) {
        if (!com.lottery.ruok.lottrey.userInfo.UserInfo.getToken().equals("")) {
            if (com.lottery.ruok.lottrey.userInfo.UserInfo.getIsTest() == 1 && com.lottery.ruok.lottrey.userInfo.UserInfo.getIsTry() != 1) {
                utils.Utils.showToast(Common.IS_TEST);
            } else {
                RetrofitBase.getRetrofit().create(GrabGiftRequest.class).getResult(content.getMsgId() + "", com.lottery.ruok.lottrey.userInfo.UserInfo.getUsername(), com.lottery.ruok.lottrey.userInfo.UserInfo.getToken())
                        .compose(RxHelper.<GrabGiftResponse>handleResult())
                        .subscribe(new RxSubscribe<GrabGiftResponse>(context) {
                            @Override
                            protected void _onNext(GrabGiftResponse grabGiftResponse) {
                                switch (grabGiftResponse.getTypes()) {
                                    case 0:
                                        utils.Utils.showToast(grabGiftResponse.getMsg());
                                        break;
                                    case 1: {
                                        //播放动画，再跳转到红包领取详情页
                                        utils.Utils.showToast(grabGiftResponse.getMsg());

                                        GrabRedPacketSucess grabRedPacketSucess = new GrabRedPacketSucess();
                                        grabRedPacketSucess.setRedPacketFrom(content.getSendUserId());
                                        grabRedPacketSucess.setHave_get_packet_id(grabGiftResponse.getGift_rand_id());
                                        grabRedPacketSucess.setRedPacketID(grabGiftResponse.getGift_id());
                                        grabRedPacketSucess.setConversationId(message.getTargetId());
                                        EventBus.getDefault().post(grabRedPacketSucess);
                                        //发送抢红包消息
                                        ReceiveRedPacketMessage receiveRedPacketMessage = new ReceiveRedPacketMessage();
                                        receiveRedPacketMessage.setMsgId(StringUtils.parseLong(grabGiftResponse.getGift_id()));
                                        receiveRedPacketMessage.setChildId(StringUtils.parseLong(grabGiftResponse.getGift_rand_id()));
                                        receiveRedPacketMessage.setDate(StringUtils.formatTime(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
                                        receiveRedPacketMessage.setMoney(StringUtils.parseDouble(grabGiftResponse.getAmount()));
                                        receiveRedPacketMessage.setReceiveUserId(com.lottery.ruok.lottrey.userInfo.UserInfo.getMyUserId());
                                        receiveRedPacketMessage.setSendUserId(content.getSendUserId());

                                        receiveRedPacketMessage.setReceiveUserNickname(com.lottery.ruok.lottrey.userInfo.UserInfo.getNickname());
                                        receiveRedPacketMessage.setReceiveUserAvatar(com.lottery.ruok.lottrey.userInfo.UserInfo.getAvatar());
                                        UserInfo sendUser = RongUserInfoManager.getInstance().getUserInfo(content.getSendUserId());
                                        if (sendUser!=null){
                                            receiveRedPacketMessage.setSendUserNickname(sendUser.getName());
                                            receiveRedPacketMessage.setSendUserAvatar(sendUser.getPortraitUri().toString());
                                        }

                                        receiveRedPacketMessage.setType(content.getType());
                                        receiveRedPacketMessage.setTargetId(content.getTargetId());
                                        RongCloudChatManager.sendMessage(content.getTargetId(), receiveRedPacketMessage);


                                        Intent intent = new Intent(context, RedPacketAniDialog.class);
                                        intent.putExtra(ChatConfig.PACKET_GET_ID, grabGiftResponse.getGift_rand_id());
                                        intent.putExtra(ChatConfig.PACKET_ID, grabGiftResponse.getGift_id());
                                        intent.putExtra(ChatConfig.CONVERSATION_ID, message.getTargetId());
                                        context.startActivity(intent);
                                        break;
                                    }
                                    case 2: {
                                        //跳转到红包领取详情页
                                        utils.Utils.showToast(grabGiftResponse.getMsg());

                                        Intent intent = new Intent(context, GetPacketResultActivity.class);
                                        intent.putExtra(ChatConfig.PACKET_GET_ID, grabGiftResponse.getGift_rand_id());
                                        intent.putExtra(ChatConfig.PACKET_ID, grabGiftResponse.getGift_id());
                                        context.startActivity(intent);
                                        break;
                                    }
                                    case 3:
                                        utils.Utils.showToast(grabGiftResponse.getMsg());
                                        break;
                                }
                            }

                            @Override
                            protected boolean showDialog() {
                                return false;
                            }
                        });
            }
        } else {
            Intent intent = new Intent(context, LoginActivity.class);
            context.startActivity(intent);
            ((Activity) context).overridePendingTransition(R.anim.anim_enter_from_bottom, R.anim.activity_stay);
        }
    }

    @Override
    public void showPrizeDetail(Context context, View view, PrizeMessage content, Message message) {
        GiveRewardDetailActivity.startActivity(context, content);
    }
}
